package com.huanxing.cloud.miniapp.config;

import com.huanxing.cloud.common.myabtis.tenant.HxTenantContextHolder;
import com.huanxing.cloud.miniapp.api.entity.WxApp;
import com.huanxing.cloud.miniapp.service.IWxAppService;
import com.huanxing.cloud.upms.api.entity.SysTenant;
import com.huanxing.cloud.upms.api.remote.RemoteTenantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class WechatConfigLoader implements ApplicationRunner {

	private final WechatConfigCache configCache;

	private final IWxAppService wxAppService; // 查询数据库的 service

	@DubboReference
	private final RemoteTenantService remoteTenantService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		List<SysTenant> tenants = remoteTenantService.list();
		if (!CollectionUtils.isEmpty(tenants)) {
			tenants.forEach(tenant -> {
				HxTenantContextHolder.setTenantId(tenant.getId());
				List<WxApp> configs = wxAppService.list();
				configCache.addConfigs(configs);
				HxTenantContextHolder.removeTenantId();
			});
			log.info("全部小程序配置信息条数：{} 条", configCache.getAllConfigs().size());

		}
	}

}
